package org.wsff.tools.api.gateway.core;

import java.io.Serializable;

/**
 * 网关过滤器抽象类
 *
 * @author ryan
 * @version Id: AbstractProxyFilter.java, v 0.1 2022-01-19 14:12 ryan Exp $$
 */
public abstract class AbstractProxyFilter<Request extends Serializable, Response extends Serializable> implements ProxyFilter<Request, Response> {

    /**
     * 代理服务过滤器
     *
     * @param context the context
     */
    @Override
    public void filter(GatewayContext<Request, Response> context) {
        if (!isEnabled(context)) {
            return;
        }
        doAccept(context);
    }

    /**
     * 执行过滤业务逻辑
     *
     * @param context the context
     */
    protected abstract void doAccept(GatewayContext<Request, Response> context);

    /**
     * 是否开启过滤器
     *
     * @param context the context
     * @return 是否开启
     */
    protected boolean isEnabled(GatewayContext<Request, Response> context) {
        return true;
    }
}
